package com.vrv.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class MapUtil {

    public static String seletTables(String tableName, List<Map<String, Object>> colsList) {
        return seletTables(tableName, colsList, 1, 10, null, null);
    }

    public static String seletTables(String tableName, List<Map<String, Object>> colsList,
            String type, List<Map<String, Object>> whereList) {
        return seletTables(tableName, colsList, 1, 10, type, whereList);
    }

    public static String seletTables(String tableName, List<Map<String, Object>> colsList,
            Integer currentPage, Integer pageCount, String type, List<Map<String, Object>> whereList) {

        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        Map<String, Object> params = getTopMap(tableInfoMap, "select_table", tableName);

        currentPage = currentPage == null ? 1 : currentPage;
        pageCount = pageCount == null ? 10 : pageCount;
        tableInfoMap.put("from", (currentPage - 1) * 10);
        tableInfoMap.put("count", pageCount);

        if (null == colsList) {
            colsList = new ArrayList<Map<String, Object>>();
            Map<String, Object> colsMap = new HashMap<String, Object>();
            colsMap.put("name", "*");
            colsMap.put("asname", "");
            colsList.add(colsMap);
        }

        tableInfoMap.put("cols", colsList);

        // 获取wheremap
        getWhereMap(type, whereList, tableInfoMap);

        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        return json;
    }

    public static String deleteTables(String tableName, String type,
            List<Map<String, Object>> whereList) {

        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        Map<String, Object> params = getTopMap(tableInfoMap, "delete_table", tableName);
        // 获取wheremap
        getWhereMap(type, whereList, tableInfoMap);

        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        return json;
    }

    public static Map<String, Object> getTopMap(Map<String, Object> tableInfoMap, String option,
            String tableName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", option);
        params.put("service", "all");
        params.put("object", "table");

        List<Map<String, Object>> tableList = new ArrayList<Map<String, Object>>();
        Map<String, Object> tableMap = new HashMap<String, Object>();
        tableMap.put("name", tableName);
        tableMap.put("asname", "");

        tableList.add(tableMap);

        tableInfoMap.put("tables", tableList);

        return params;
    }

    private static void getWhereMap(String type, List<Map<String, Object>> whereList,
            Map<String, Object> tableInfoMap) {

        if (null != whereList && !whereList.isEmpty()) {
            List<Map<String, Map<String, Object>>> tmepList = new ArrayList<Map<String, Map<String, Object>>>();
            for (Map<String, Object> whereMap : whereList) {
                Map<String, Map<String, Object>> abMap = new HashMap<String, Map<String, Object>>();

                for (String key : whereMap.keySet()) {
                    if ("type".equals(key)) {
                        abMap.put(whereMap.get(key) + "", whereMap);
                        whereMap.remove(key);
                    }
                }
                tmepList.add(abMap);
            }
            Map<String, Object> typeMap = new HashMap<String, Object>();
            type = StringUtils.isEmpty(type) ? "must" : type;
            typeMap.put(type, tmepList);
            Map<String, Object> boolMap = new HashMap<String, Object>();
            boolMap.put("bool", typeMap);
            tableInfoMap.put("where", boolMap);
        }
    }
}
